<template>
  <a-modal
    :title="title"
    :width="width"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭">
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">

<!--        <a-form-item label="学院" :labelCol="labelCol" :wrapperCol="wrapperCol">-->
<!--          <j-select-depart v-decorator="['college']" :trigger-change="true"/>-->
<!--        </a-form-item>-->
        <a-form-item label="学生姓名" :labelCol="labelCol" :wrapperCol="wrapperCol">
         <!-- <j-dict-select-tag
            v-decorator="['stName']"
            :trigger-change="true"
            org-fields="st_name,st_name"
            dest-fields="st_name,st_name"
            code="st_student"
            @callback="popupCallback"/>-->
          <j-search-select-tag type="list"  url="/st/mobile/getStudentByStuIdOrId" :async="true"  :id="stName"  v-decorator="['stName']" :trigger-change="true" dict="st_student,st_name,id" placeholder="请选择学生"/>
        </a-form-item>
        <a-form-item label="原因" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <j-dict-select-tag type="list" v-decorator="['theReason']" :trigger-change="true" dictCode="st_status" placeholder="请选择原因"/>
        </a-form-item>
        <a-form-item label="离校时间" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <j-date placeholder="请选择离校时间" v-decorator="[ 'departureTime', validatorRules.departureTime]" :trigger-change="true" :show-time="true" date-format="YYYY-MM-DD HH:mm:ss" style="width: 100%"/>
        </a-form-item>
        <a-form-item label="备注" :labelCol="labelCol" :wrapperCol="wrapperCol">
<!--          <j-input v-decorator="['fieldOne']" :trigger-change="true"/>-->
          <a-input v-decorator="[ 'fieldOne', validatorRules.fieldOne]" placeholder="请输备注"></a-input>
        </a-form-item>
        <a-form-item label="审批人" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <j-select-user-by-dep-i-d v-decorator="['approver']" :trigger-change="true"/>
        </a-form-item>
        <a-form-item label="归校时间" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <j-date placeholder="请选择归校时间" v-decorator="[ 'backTime', validatorRules.backTime]" :trigger-change="true" :show-time="true" date-format="YYYY-MM-DD HH:mm:ss" style="width: 100%"/>
        </a-form-item>
        <a-form-item label="确认人" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <j-select-user-by-dep-i-d v-decorator="['confirmor']" :trigger-change="true"/>
        </a-form-item>
        <a-form-item label="确认时间" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <j-date placeholder="请选择确认时间" v-decorator="[ 'confirmTime', validatorRules.confirmTime]" :trigger-change="true" :show-time="true" date-format="YYYY-MM-DD HH:mm:ss" style="width: 100%"/>
        </a-form-item>

      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>

  import { httpAction } from '@/api/manage'
  import pick from 'lodash.pick'
  import JDate from '@/components/jeecg/JDate'  
  import JSelectUserByDepID from '@/components/jeecgbiz/JSelectUserByDepID'
  import JSearchSelectTag from "@/components/dict/JSearchSelectTag"
  import JSelectDepart from '@/components/jeecgbiz/JSelectDepart'
  import JDictSelectTag from "@/components/dict/JDictSelectTag"
  import JTreeSelect from '@/components/jeecg/JTreeSelect'

  export default {
    name: "StStateJiluModal",
    components: {
      JDate,
      JSelectUserByDepID,
      JSearchSelectTag,JDictSelectTag,JTreeSelect,JSelectDepart
    },

    data () {
      return {
        stName:"",
        form: this.$form.createForm(this),
        title:"操作",
        width:800,
        visible: false,
        model: {},
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },

        confirmLoading: false,
        validatorRules:{
        college:{rules: [{ required: true, message: '请输入学院!' }]},
        stName:{rules: [{ required: true, message: '请输入学生姓名!' }]},
        theReason:{rules: [{ required: true, message: '请输入原因!' }]},
        departureTime:{rules: [{ required: true, message: '请输入离校时间!' }]},
        approver:{rules: [{ required: true, message: '请输入审批人!' }]},
        backTime:{rules: [{ required: true, message: '请输入归校时间!' }]},
        confirmor:{},
        confirmTime:{},
        },
        url: {
          add: "/st/stStateJilu/add",
          edit: "/st/stStateJilu/edit",
        }
     
      }
    },
    created () {
      console.log("this.id",this.stName)
    },  mounted () {
      console.log("this.id",this.stName)
    },
    methods: {
      add () {
        this.edit({});
      },
      edit (record) {
        console.log(record)

        this.form.resetFields();
        this.model = Object.assign({}, record);
        this.visible = true;
        this.stName=record.stName
        this.$nextTick(() => {
          this.form.setFieldsValue(pick(this.model,'college','stName','theReason','departureTime','approver','backTime','confirmor','confirmTime'))
        })

      },
      close () {
        this.$emit('close');
        this.visible = false;
      },
      handleOk () {
        const that = this;
        // 触发表单验证
        this.form.validateFields((err, values) => {
          if (!err) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            let formData = Object.assign(this.model, values);
            console.log("表单提交数据",formData)
            httpAction(httpurl,formData,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
              that.close();
            })
          }
         
        })
      },
      handleCancel () {
        this.close()
      },
      popupCallback(row){
        this.form.setFieldsValue(pick(row,'college','stName','theReason','departureTime','approver','backTime','confirmor','confirmTime'))
      },

      
    }
  }
</script>